<?php
namespace app\api\controller\robat;

use think\Controller;
use app\common\model\user\User;
use app\api\controller\mytool\Mytool;
use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\sendmsg\Sendmsg;
use app\common\model\sendmsg\Sendmsglog;
use app\api\controller\mytool\Sendmessage as Sm;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\company\Contract;
use app\common\model\cost\Costjournal;
use app\common\model\orderhawa\HawaList;
use app\common\model\orderdelivery\DeliveryList;
use app\common\model\orderreceipt\ReceiptList;
class Sendmessage extends Base
{
    public function index(){
        self::waitlistrun();
    }

    
    /**
     * 推送每天周报情况
     * @param  [type] $time [description]
     * @return [type]       [description]
     */
    public function ordersummer($time){
        $t = strtotime('2022-05-31');
        switch ($time) {
            case 'day':
                $title = '日报已生成';
                $now = date('Y-m-d',$t);
                $end = date('Y-m-d',$t);
                break;
            case 'week':
                $title = '周报已生成';
                $week = date('w', $t);
                $now = date('Y-m-d' ,strtotime( '+' . (1-$week) .' days', $t));
                $end = date('Y-m-d',$t);
                break;
            case 'month':
                $title = '月报已生成';
                $now = date('Y-m',$t).'-01';
                $end = date('Y-m',$t).'-31';
                break;
        }
        $data = self::summary($now,$end);
        $str = '';
        foreach ($data['summary'] as $key => $value) {
            if(!empty($value[1])){
                $str = $str . $value[0].$value[1].$value[2];
                if(!empty($value[5])){
                    $str = $str.$value[5].'；';
                }else{
                    $str = $str . '；';
                }
            }
        }
        if(empty($str)){
            return $title.'没有动态无需发送信息';
        }
        //通知权限查看概况的人
        $rdata['temp']    = WECHATTEMP1;
        $rdata['msgdata'] = array('first' => $title,
                            'keyword1' =>$now.($now!==$end?'~'.$end:'当日'),
                            'keyword2' =>$str,
                            'remark' =>'点击查看详细信息',
                            'url'=>WEBURL.'summary/order?starttime='.$now.'&endtime='.$end,
                        );
        $rdata['touser']['SendPowerCode'] = 204002;
        //将消息发送出去
        Sm::send($rdata,'summary',true);
        return $title.'发送成功';
    }


    /**
     * 获取待推送列表
     * @return [type] [description]
     */
    public function waitlistrun(){
        $today = date('Y-m-d H:i:s',time());
        $data = Sendmsg::where('SendTime','<',$today)->where('SendCount','<',3)->select();
        foreach ($data as $key => $value) {
            $sb = Sm::send(json_array($value['Content'],true),$value['MsgType'],true);
            if($sb['code']){
                $ndata = new Sendmsglog;
                $nvalue = $value->toArray();
                $sb = $ndata->save($nvalue);
                $value->delete();
            }else{
                switch ($value->SendCount) {
                    case 0:
                        $addtime = 300;//5分钟
                        break;
                    case 1:
                        $addtime = 1800;//15分钟
                        break;
                    case 2:
                        $addtime = 3600;//60分钟
                        break;
                    case 3:
                        $addtime = 7200;//120分钟
                        break;
                    case 4:
                        $addtime = 10800;//180分钟
                        break;
                    default:
                        $addtime = 0;//180分钟
                        break;
                }
                $value->SendCount = $value->SendCount +1;
                $value->SendTime = date('Y-m-d H:i',strtotime($value->SendTime)+$addtime);
                $value->ErrorLog =  date('Y-m-d H:i',time()).':'.$sb['msg'] ."\n".$value->ErrorLog;
                $value->save();
            }
        }
        return sok('等待列表运行正常');
    }


    public function summary($now,$end){
        $timestr = $now .'~' .$end;
        //统计数据
            $sb = Costjournal::getsummary($now,$end);
            if(!empty($sb)){
                $rdata['summary'][] = ['合计支出',round($sb['PayCost']),$sb['PayCostUnit'],$timestr.'支出','public/list?type=costpay&starttime='.$now.'&endtime='.$end];
                $rdata['summary'][] = ['合计收入',round($sb['RevenueCost']),$sb['RevenueCostUnit'],$timestr.'收入','public/list?type=costrevenue&starttime='.$now.'&endtime='.$end];
            }
        $sb = HawaList::getsummary($now,$end);
        if(!empty($sb)){
            if(empty($sb['wait'])){
                $rdata['summary'][] = ['产废回收',$sb['Weight'],$sb['WeightUnit'],$timestr.'回收','public/list?type=manaorderhawa&starttime='.$now.'&endtime='.$end];
            }else{
                $rdata['summary'][] = ['产废回收',$sb['Weight'],$sb['WeightUnit'],$timestr.'回收','public/list?type=manaorderhawa&starttime='.$now.'&endtime='.$end,$sb['wait'].'笔待结算'];  
            }
        }
        $sb = ReceiptList::getsummary($now,$end);
        if(!empty($sb)){
            if(empty($sb['wait'])){
                $rdata['summary'][] = ['入库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'入库','public/list?type=manaorderreceipt&starttime='.$now.'&endtime='.$end];
            }else{
                $rdata['summary'][] = ['入库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'入库','public/list?type=manaorderreceipt&starttime='.$now.'&endtime='.$end,$sb['wait'].'笔待结算'];
            }
        }
       
        $sb = DeliveryList::getsummary($now,$end);
        if(!empty($sb)){
            if(empty($sb['wait'])){
                $rdata['summary'][] = ['出库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'出库','public/list?type=manaorderdelivery&starttime='.$now.'&endtime='.$end];
            }else{
                $rdata['summary'][] = ['出库订单',$sb['Weight'],$sb['WeightUnit'],$timestr.'出库','public/list?type=manaorderdelivery&starttime='.$now.'&endtime='.$end,$sb['wait'].'笔待结算'];
            }
            
        }
       
        $sb = Contract::getsummary($now,$end);
        if(!empty($sb)){
            $rdata['summary'][] = ['处置合同',$sb['Count'],$sb['CountUnit'],$timestr.'合同','public/list?type=manaorderdelivery&starttime='.$now.'&endtime='.$end,'合计'.$sb['Price'].$sb['PriceUnit']];
        }
        
        //入库情况
        $sb = ReceiptList::getsummaryinfo($now,$end);
        if(!empty($sb)){
            //每日情况
            $rdata['datalist'] = $sb['datalist'];
            //收集员数据        
            $rdata['userlist'] = ['namelist'=>$sb['namelist'],'data'=>$sb['userlist']];
        }
        return $rdata;
    }
}
